<?php
require_once('./admin/connexion.php');
require ('./func.req.php');
if ($install) {
    header("Location:./install/index.php");
} else {
    require('./config/tbl.php');
    if ($line_info['maintenance_choix_info'] == "Oui") {
        show_maintenance();
    } else {
        /*
          -----------------------------------
          ---- SCRIPT D'AUTHENTIFICATION ----
          FujiCraft V3.0
          By Fujido
          Contact : thefujido@gmail.com
          -----------------------------------
         */

        session_start(); // On relaye la session
        if (@session_is_registered("authentification")) { // v�rification sur la session authentification (la session est elle enregistr�e ?)
// ici les �ventuelles actions en cas de r�ussite de la connexion
        }
// GESTION DE LA D�connexion
        if (isset($_GET['erreur']) && $_GET['erreur'] == 'logout') { // Test sur les param�tres d'URL qui permettront d'identifier un "contexte" de d�connexion
            $prenom = $_SESSION['prenom']; // On garde le pr�nom en variable pour dire au revoir (soyons polis :-)
            @session_unset("authentification");
            header("Location:login.php?erreur=logout");
        }
        ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <title><?php echo $site; ?> | Commentaire</title>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <meta http-equiv="Content-Style-Type" content="text/css" />
                <meta http-equiv="content-language" content="fr,en" />
                <link rel='shortcut icon' type="image/x-icon" href='./images/favicon.ico' />
                <link rel="stylesheet" type="text/css" href="./css/style.css" title="" media="all" />
                <link rel="stylesheet" type="text/css" href="./css/news.css" title="" media="all" />
                <link rel="stylesheet" type="text/css" href="./css/comment.css" title="" media="all" />
                <link rel="stylesheet" type="text/css" href="./css/bbcode.css" title="" media="all" />
                <script type="text/javascript" src="./js/news.js"></script>
                <script type="text/javascript" src="./js/bbcode.js"></script>
            </head>
            <body>
                <div id="block">
                    <div id="contentblock">
                        <div class="wrapper">
                            <?php
                            show_header();
                            show_menu();
                            show_newsbox();
                            show_userbox();
                            //Selection de la table
                            $id_news = $_GET['news'];
                            $query_news = "SELECT * FROM " . $database . "." . $tb_news . " WHERE id = '$id_news'  ";
                            //Affichage de la selection
                            $result_news = @mysql_query($query_news) OR DIE("Erreur de la s&eacute;lection des utilisateurs : " . mysql_error());
                            $line_news = mysql_fetch_assoc($result_news);

                            $query_com = "SELECT * FROM " . $database . "." . $tb_comment . " WHERE id_news_com = '$id_news'  ";
                            //Affichage de la selection
                            $result_com = @mysql_query($query_com) OR DIE("Erreur de la s&eacute;lection du commentaire : " . mysql_error());
                            ?>
                            <div class="mainbox">
                                <h2>Commentaire de la news "<i class="black"><?php echo addChar(utf8_encode($line_news['title'])); ?></i>"</h2>
                                <br />
                                <center>
                                    <?php
                                    $date_usa_news = $line_news['date'];
                                    $date_news = substr($date_usa_news, 8, 10) . "/" . substr(substr($date_usa_news, 5, 7), 0, 2) . "/" . substr($date_usa_news, 0, 4);
                                    ?>
                                    <br />
                                    <div class="news_titre rounded">
                                        <div class="news_title"><?php echo addChar(utf8_encode($line_news['title'])); ?></div>
                                        <div class="news_info">Ajout&eacute; le: <img src="./images/icone/date.png" alt="date" /> <?php echo $date_news; ?>, par <b><a href="./user.php?user=<?php echo $line_news['author']; ?>" title="Voir le profil de <?php echo $line_news['author']; ?>"><img src="./images/icone/author.png" alt="auteur" /> <?php echo $line_news['author']; ?></a></b></div> <br />
                                    </div>
                                    <div class="news_txt">
                                        <?php echo addChar(utf8_encode($line_news['content'])); ?> <br />
                                        <br />
                                    </div>
                                    <hr />
                                    <br />
                                    <?php
                                    while ($line = mysql_fetch_assoc($result_com)) {
                                        $date_usa = $line['date_com'];
                                        $date = substr($date_usa, 8, 10) . "/" . substr(substr($date_usa, 5, 7), 0, 2) . "/" . substr($date_usa, 0, 4);
                                        $heure_usa = $line['heure_com'];
                                        $heure = substr($heure_usa, 0, 2) . "h" . substr($heure_usa, 3, 5);
                                        ?>
                                        <div class="comment-box rounded">
                                            Post&eacute; par <b><a href="./user.php?user=<?php echo $line['author_com']; ?>" title="Voir le profil de <?php echo $line['author_com']; ?>"><?php echo $line['author_com']; ?></a></b>, le <i><?php echo $date; ?></i> &agrave; <i><?php echo $heure; ?></i><br />
                                            <br />
                                            <?php echo addChar(utf8_encode($line['comment_com'])); ?> <br />
                                        </div>
                                    <?php } ?>
                                    <br />
                                    <br />
                                    <br />
                                    <br />
                                    <?php
                                    if (@session_is_registered("authentification")) { // Affichage conditionnel : si et seulement si l'utilisateur est connect&eacute; avec le privilege utilisateur simple
                                        ?>
                                        <form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
                                            <table id="pattern-style-b" summary="Commentaire">
                                                <thead>
                                                    <th colspan="3" scope="col"><b>Laissez un commentaire</b></th>
                                                </thead>
                                                <?php show_bbcode_box('comment') ?>
                                                <tbody>
                                                    <tr>
                                                        <td>Message : </td><td>&nbsp;</td><td colspan="1"><textarea cols="40" rows="5"  name="comment" id="comment"></textarea></td>
                                                    </tr>
                                                    <tr>
                                                        <td>&nbsp;</td>
                                                        <td>&nbsp;</td><td><input type="submit" name="add_comment" class="btn" value="Envoyer"/></td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </form>
                                        <?php
                                    } else {
                                        ?>
                                        <div id="box" onclick="document.getElementById('box').style.display='none';" class="info-box rounded" title="Fermer la fen&ecirc;tre">
                                            Vous devez &ecirc;tre connect&eacute; afin d'ajouter un commentaire.
                                        </div>
                                        <?php
                                    }
                                    ?>

                                    <?php
                                    if (isset($_POST['add_comment']) && $_POST['comment'] != "") {
                                        $comment = htmlspecialchars($_POST['comment']);
                                        $id_news_com = $_GET['news'];
                                        $author_com = $_SESSION['login'];
                                        $date_com = date('y') . "-" . date('m') . "-" . date('j');
                                        $heure_com = date('H') . ":" . date('i');
                                        $comment_com = mysql_escape_string($comment);
                                        $add_com = sprintf("INSERT INTO " . $database . "." . $tb_comment . " (id_news_com, author_com, date_com, comment_com, heure_com) VALUES ('$id_news_com', '$author_com', '$date_com', '$comment_com', '$heure_com')");
                                        mysql_select_db($database, $dbconnect);
                                        $result = mysql_query($add_com, $dbconnect) or die(mysql_error());
                                        ?>
                                        <div id="box" onclick="document.getElementById('box').style.display='none';" class="success-box rounded" title="Fermer la fen&ecirc;tre">
                                            Le commentaire a bien &eacute;t&eacute; ajout&eacute;.
                                        </div>
                                        <?php
                                    } else if (isset($_POST['add_comment']) && $_POST['comment'] == "") {
                                        ?>
                                        <div id="box" onclick="document.getElementById('box').style.display='none';" class="error-box rounded" title="Fermer la fen&ecirc;tre">
                                            &Eacute;chec de l'enregistrement. Veuillez entrez un commentaire.
                                        </div>
                                        <?php
                                    }
                                    ?>
                                </center>
                                <br />
                            </div>
                            <?php show_footer(); ?>
                        </div>
                    </div>
                </div>
            </body>
        </html>
        <?php
    }
}
?>